package com.example.paper.mvp.view;

import android.graphics.Color;
import android.view.View;

import androidx.fragment.app.FragmentTransaction;

import com.example.mvp.view.BaseActivity;
import com.example.paper.R;
import com.example.paper.fragment.LiveBroadcastFragment;
import com.example.paper.fragment.MeFragment;
import com.example.paper.fragment.NewsFragment;
import com.example.paper.fragment.PeopleFragment;
import com.example.paper.fragment.VideoFragment;
import com.example.paper.mvp.contract.FrameContract;
import com.example.paper.mvp.presenter.FramePresenter;
import com.example.paper.widget.BottomButton;

import java.util.Map;

public class AllActivity extends BaseActivity<FramePresenter> implements FrameContract.IFrameView, BottomButton.SelectListener {

    private BottomButton news;
    private BottomButton People;
    private BottomButton video;
    private BottomButton broadcast;
    private BottomButton me;
    private NewsFragment newsFragment;//第一个
    private PeopleFragment peopleFragment;//第二个
    private VideoFragment videoFragment;//第三个
    private LiveBroadcastFragment liveBroadcastFragment;//第四个
    private MeFragment meFragment;//第五个



    @Override
    public int bindLayout() {
        return R.layout.activity_all;
    }

    @Override
    public void initView() {
        //获取id
        news = (BottomButton) findViewById(R.id.news);
        People = (BottomButton) findViewById(R.id.People);

        video = (BottomButton) findViewById(R.id.video);
        broadcast = (BottomButton) findViewById(R.id.broadcast);

        me = (BottomButton) findViewById(R.id.me);

    }

    @Override
    public void initData() {
//        initFragment();
        //添加布局
        newsFragment = new NewsFragment();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.addFragment,newsFragment);
        fragmentTransaction.show(newsFragment);
        fragmentTransaction.commit();

        //点击事件调用
        news.setContent("新闻")
                .setNormaColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu1_f)
                .setSelImg(R.drawable.tu1_t)
                .setListener(this::onSelect)
                .setShowPoint(false);

        People.setContent("人民号")
                .setNormaColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu2_f)
                .setSelImg(R.drawable.tu2_t)
                .setListener(this::onSelect)
                .setShowPoint(false);

        video.setContent("视频")
                .setNormaColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu3_f)
                .setSelImg(R.drawable.tu3_t)
                .setListener(this::onSelect)
                .setShowPoint(false);

        broadcast.setContent("直播")
                .setNormaColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu4_f)
                .setSelImg(R.drawable.tu4_t)
                .setListener(this::onSelect)
                .setShowPoint(false);

        me.setContent("我的")
                .setNormaColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNormalImg(R.drawable.tu5_f)
                .setSelImg(R.drawable.tu5_t)
                .setListener(this::onSelect)
                .setShowPoint(false);

    }

//    private void initFragment(int p) {
//        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//        ArrayList<Fragment> fragments = new ArrayList<>();
//        for (int i = 0; i < fragments.size(); i++) {
//            if (i==p){
//                fragmentTransaction.show(fragments.get(i));
//            }else {
//                fragmentTransaction.hide(fragments.get(i));
//            }
//        }
//        fragmentTransaction.commit();
//    }


    @Override
    public View addStatusView() {
        return findViewById(R.id.ll);
    }

    @Override
    public void showToast(String s) {

    }

    public void hideFragment(){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (newsFragment != null){
            fragmentTransaction.hide(newsFragment);
        }
        if (peopleFragment != null){
            fragmentTransaction.hide(peopleFragment);
        }
        if (videoFragment != null){
            fragmentTransaction.hide(videoFragment);
        }
        if (liveBroadcastFragment != null){
            fragmentTransaction.hide(liveBroadcastFragment);
        }
        if (meFragment != null){
            fragmentTransaction.hide(meFragment);
        }
        fragmentTransaction.commit();
    }

    public void showFragment(int id){
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        switch (id){
            case R.id.news:
                fragmentTransaction.show(newsFragment);
                break;
            case R.id.People:
                if (peopleFragment == null){
                    peopleFragment = new PeopleFragment();
                    fragmentTransaction.add(R.id.addFragment,peopleFragment);
                }else {
                    fragmentTransaction.show(peopleFragment);
                }
                break;
            case R.id.video:
                if (videoFragment == null){
                    videoFragment = new VideoFragment();
                    fragmentTransaction.add(R.id.addFragment,videoFragment);
                }else {
                    fragmentTransaction.show(videoFragment);
                }
                break;
            case R.id.broadcast:
                if (liveBroadcastFragment == null){
                    liveBroadcastFragment = new LiveBroadcastFragment();
                    fragmentTransaction.add(R.id.addFragment,liveBroadcastFragment);
                }else {
                    fragmentTransaction.show(liveBroadcastFragment);
                }
                break;
            case R.id.me:
                if (meFragment == null){
                    meFragment = new MeFragment();
                    fragmentTransaction.add(R.id.addFragment,meFragment);
                }else {
                    fragmentTransaction.show(meFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }


    @Override
    public void onSelect(int id) {
        //选择判断
        if (id == R.id.news) {
            People.clearSelect();
            video.clearSelect();
            broadcast.clearSelect();
            me.clearSelect();
            hideFragment();
            showFragment(R.id.news);
            status_show(Color.WHITE);
        } else if (id == R.id.People) {
            news.clearSelect();
            video.clearSelect();
            broadcast.clearSelect();
            me.clearSelect();
            hideFragment();
            showFragment(R.id.People);
            status_show(Color.WHITE);
        } else if (id == R.id.video) {
            news.clearSelect();
            People.clearSelect();
            broadcast.clearSelect();
            me.clearSelect();
            hideFragment();
            showFragment(R.id.video);
            status_show(Color.WHITE);
        } else if (id == R.id.broadcast) {
            news.clearSelect();
            People.clearSelect();
            video.clearSelect();
            me.clearSelect();
            hideFragment();
            showFragment(R.id.broadcast);
            status_show(Color.WHITE);
        } else if (id == R.id.me) {
            news.clearSelect();
            People.clearSelect();
            video.clearSelect();
            broadcast.clearSelect();
            hideFragment();
            showFragment(R.id.me);
            status_show(Color.RED);
        }
    }

    @Override
    public void refresh(Map<String, Object> resultMap) {

    }
}